package com.hcg.multidatasourceofanno2.common.function;

import com.hcg.multidatasourceofanno2.common.utils.JudgeUtils;

public class CommonException extends RuntimeException {

    private static final long serialVersionUID = 5539431043384054654L;
    public static final String SYS_ERROR_MSGCD = ErrorMsgCode.SYS_ERROR.getMsgCd();

    private String msgCd;
    private String msgInfo;
    private Throwable cause;
    private boolean businessException = false;

    public CommonException(String msgCd, String msgInfo, Throwable cause) {
        super(msgCd + " : " + msgInfo, cause);
        this.msgCd = msgCd;
        this.msgInfo = msgInfo;
        this.cause = cause;
    }

    public CommonException(String msgCd, String msgInfo) {
        super(msgCd + " :  " + msgInfo);
        this.msgCd = msgCd;
        this.msgInfo = msgInfo;
    }

    public CommonException(ErrorMsgCode errorMsgCode) {
        this(errorMsgCode.getMsgCd(), errorMsgCode.getMsgInfo());
    }

    public CommonException(ErrorMsgCode errorMsgCode, Throwable cause) {
        this(errorMsgCode.getMsgCd(), errorMsgCode.getMsgInfo(), cause);
    }

    public CommonException(String msgCd) {
        super(msgCd);
        this.msgCd = msgCd;
    }

    public CommonException(String msgCd, boolean businessException) {
        super(msgCd);
        this.msgCd = msgCd;
        this.businessException = businessException;
    }

    public CommonException(Throwable cause) {
        super(SYS_ERROR_MSGCD, cause);
        this.cause = cause;
        this.msgCd = SYS_ERROR_MSGCD;
    }

    public CommonException(String msgInfo, Throwable cause) {
        super(SYS_ERROR_MSGCD + " : " + msgInfo);
        this.msgCd = SYS_ERROR_MSGCD;
        this.msgInfo = msgInfo;
        this.cause = cause;
    }

    public CommonException() {
        super(SYS_ERROR_MSGCD);
        this.msgCd = SYS_ERROR_MSGCD;
    }

    public static void throwBusinessException(String msgCd) {
        throw new CommonException(msgCd, true);
    }

    public static void throwLemonException() {
        throw new CommonException();
    }

    public static void throwLemonException(String msgCd) {
        throw new CommonException(msgCd);
    }

    public static void throwLemonException(Throwable t) {
        if ((t instanceof CommonException)) {
            throw ((CommonException) t);
        }
        throw new CommonException(t);
    }

    public static void throwLemonException(String msgInfo, Throwable t) {
        throw new CommonException(msgInfo, t);
    }

    public static void throwLemonException(String msgCd, String msgInfo) {
        throw new CommonException(msgCd, msgInfo);
    }

    public static void throwCommonException(String msgCd, String msgInfo, Throwable throwable) {
        throw new CommonException(msgCd, msgInfo, throwable);
    }

    public static void throwCommonException(ErrorMsgCode errorMsgCode) {
        throw new CommonException(errorMsgCode.getMsgCd(), errorMsgCode.getMsgInfo());
    }

    public static void throwCommonException(ErrorMsgCode errorMsgCode, Throwable throwable) {
        throw new CommonException(errorMsgCode.getMsgCd(), errorMsgCode.getMsgInfo(), throwable);
    }

    public static CommonException create(Throwable t) {
        if (isCommonException(t)) {
            return (CommonException) t;
        }
        return new CommonException(t);
    }

    public static CommonException create(String msgCd) {
        return new CommonException(msgCd);
    }

    public static CommonException create(String msgCd, String msgInfo) {
        return new CommonException(msgCd, msgInfo);
    }

    public static CommonException create(ErrorMsgCode errorMsgCode) {
        return new CommonException(errorMsgCode.getMsgCd(), errorMsgCode.getMsgInfo());
    }

    public static CommonException create(ErrorMsgCode errorMsgCode, Throwable throwable) {
        return new CommonException(errorMsgCode.getMsgCd(), errorMsgCode.getMsgInfo(), throwable);
    }

    public static boolean isCommonException(Throwable throwable) {
        if ((JudgeUtils.isNotNull(throwable)) && ((throwable instanceof CommonException))) {
            return true;
        }
        return false;
    }

    public String getMsgCd() {
        return this.msgCd;
    }

    public void setMsgCd(String msgCd) {
        this.msgCd = msgCd;
    }

    public String getMsgInfo() {
        return this.msgInfo;
    }

    public void setMsgInfo(String msgInfo) {
        this.msgInfo = msgInfo;
    }

    public Throwable getCause() {
        return this.cause;
    }

    public void setCause(Throwable cause) {
        this.cause = cause;
    }

    public boolean isBusinessException() {
        return this.businessException;
    }

    public void setBusinessException(boolean businessException) {
        this.businessException = businessException;
    }
}